package com.corechina.apex_osswords.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理器
 * @author cc
 */
@RestControllerAdvice
@Slf4j
public class HandlerExceptionConstant {

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(APEXOSSWordsException.class)
	public Response HDAhmsHandlerException(APEXOSSWordsException e){
		Response r = new Response(e.getCode(),e.getMessage(),null,1,false);
		return r;
	}

	@ExceptionHandler(DuplicateKeyException.class)
	public Response handleDuplicateKeyException(DuplicateKeyException e){
		log.error(e.getMessage(), e);
		return new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(),"数据库中已存在该记录",null,1,false);
	}

	@ExceptionHandler(AuthorizationException.class)
	public Response handleAuthorizationException(AuthorizationException e){
		log.error(e.getMessage(), e);
		return new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(),"没有权限，请联系管理员授权",null,1,false);
	}

	@ExceptionHandler(Exception.class)
	public Response handleException(Exception e){
		log.error(e.getMessage(), e);
		return new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(),"未知异常，请联系管理员!",null,1,false);
	}
}
